<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>Amazing - part 5</title>
</head>
<body>
<h2 class="title">21. Amazing - part 5</h2>
<h3 class="section">More complications</h3>
<p>Consider the following world</p>
<p><img alt="around the world: start" src=
"../../images/intro/around4start.png"></p>
<p>Does the program written previously work with it? Try it!</p>
<p>As you probably guessed, if you didn't try it <small>(you should,
really!)</small>, it does not. To make it work, we need to replace the
<span class="pykeyword">if</span> we just added by a <span class=
"pykeyword">while</span>. Try it! Make sure to save it.</p>
<!--=====================================-->
<hr width="50%">
<h3 class="section">Clarifying our intent</h3>
<p>We seem to have designed a program that works in all situations we are
likely to encounter. This program, before we forget, is to allow Reeborg to
explore his world, going around once. While the program is rather short, and
its structure should be clear at this point, it might not be so obvious to
someone who just happened to see it for the first time. It's probably a good
idea either to add comments and/or to introduce more meaningful words. Let's
start by adding comments, somewhat more verbose than we think we might
need.</p>
<pre>
<span class="comment"># we define a useful instruction</span>
<span class="keyword">def</span> turn_right():
    repeat(turn_left, 3)

<span class=
"comment"># we mark the starting point by putting down a beeper</span>
put_beeper()

<span class=
"comment"># We then find a clear direction and start moving.</span>
<span class="keyword">while not</span> front_is_clear():
    turn_left()
move()

<span class="comment"># We know we have gone around the world</span>
<span class="comment"># when we come back to the beeper we put down.</span>

<span class="keyword">while not</span> on_beeper():
    <span class="keyword">if</span> right_is_clear(): <span class=
"comment"># keep to the right</span>
        turn_right()
        move()
    <span class="keyword">elif</span> front_is_clear(): <span class=
"comment"># move following the right wall</span>
        move()
    <span class="keyword">else</span>:<span class=
"comment"> # follow the wall by turning left</span>
        turn_left()

turn_off()
</pre>
<p>While this sort of clarifies our intent for each instruction, it is not
really that helpful in summarizing the method (also known as the
<i>algorithm</i>) used in solving the problem. Therefore, these comments
might not be as helpful to another reader as we might have wished. Reading
over the comments, we note that the program has two parts:<br>
1) mark the starting point;<br>
2) follow the right wall until we come back to the start.<br>
Let's rewrite this program so that these two parts become clearer, and
writing the comments differently.</p>
<pre>
<span class=
"comment"># This program instructs Reeborg to go around his world</span>
<span class="comment"># counterclockwise, stopping when he comes </span>
<span class="comment"># back to his starting point.</span>

<span class="keyword">def</span> turn_right():
    repeat(turn_left, 3)

<span class="keyword">def</span> mark_starting_point_and_move():
    put_beeper()
    <span class="keyword">while not</span> front_is_clear():
        turn_left()
    move()

<span class="keyword">def</span> follow_right_wall():
    <span class="keyword">if</span> right_is_clear(): 
        turn_right()
        move()
    <span class="keyword">elif</span> front_is_clear(): 
        move()
    <span class="keyword">else</span>: 
        turn_left()

found_starting_point = next_to_a_beeper   <span class=
"comment"># beeper marks the starting point.</span>

<span class="comment">#=== End of definitions; begin solution</span>

mark_starting_point_and_move()
            
<span class="keyword">while not</span> found_starting_point(): 
    follow_right_wall()
      
turn_off()
</pre>
<p>Isn't this clearer? Now, suppose that each corner has a beeper on it at
the start. We could then choose to mark the starting point by <i>removing</i>
a beeper. We would need to make some small changes in the definitions, but
the solution as such would not need to be changed.</p>
<!--======================================-->
<hr width="50%">
<h3 class="try">First surprise</h3>
<p>Change the program you just wrote to remove the <tt>put_beeper()</tt>
instruction.  After saving it, try this slightly modified program 
with the following hurdle race (file: hurdles3.wld).</p>
<p><img alt="hurdles" src="../../images/intro/hurdles3_start.png"></p>
<p>Surprise! Other than finishing facing in an unusual direction (perhaps
bowing to the crowd after winning), the program we just wrote can solve the
hurdle problem. It also works with the uneven hurdle (file: hurdles4.wld)
illustrated below - which the program we wrote before for jumping over
hurdles could not handle!</p>
<p><img alt="hurdles" src="../../images/intro/hurdles4_start.png"></p>
<!--======================================-->
<hr width="50%">
<h3 class="try">Amazing surprise</h3>
<p>Try the same modified program again with the maze 
problem we introduced in one of the first lessons (file: maze1.wld), 
as illustrated below.</p>
<p><img alt="maze" src="../../images/intro/maze1_start.png"></p>
<p>As you will see, our simple program can find the exit to this maze!
Amazing!</p>
<!--======================================-->
<hr width="50%">
<h3 class="section">Conclusion</h3>
<p>We started with a simple problem to solve (going around a rectangular
world) and, by improving little by little (also called <i>stepwise
refinement</i>), we manage to write a program that could be used to solve
many other apparently unrelated problems. At each step, we kept the changes
small, and made sure we had a working solution, before considering more
complex problems. We also used more descriptive names for parts of the
<i>algorithm</i> which made the program easier to read and, hopefully, to
understand. This is a strategy you should use when writing your own
programs:</p>
<dl>
<dt><b>Rule # 5</b></dt>
<dd>Steps to follow when writing a program:</dd>
<ul>
<li>start simple;</li>
<li>introduce small changes, one at a time;</li>
<li>make sure that each of the changes you have introduced do not invalidate
the work you have done before;</li>
<li>add appropriate comments that don't simply repeat what each instruction
does; and</li>
<li>choose descriptive names.</li>
</dl>
</ul>
<center><a href="20-amazing4.htm"><img alt="previous" src=
"../../images/previous.png"> Amazing - part4</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"22-rain.htm">It's raining! <img alt="next" src=
"../../images/next.png"></a>
</center>
</body>
</html>
